﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using UIWindows.Models;

namespace UIWindows.GlobalStatics
{
    [Attributes.Singleton]
    public class DisplayItemsStatic
    {
        /// <summary>
        /// 主页是否更新列表
        /// </summary>
        public bool IsHomeUpdate { get; set; }
        /// <summary>
        /// 数据页面是否更新列表
        /// </summary>
        public bool IsDataUpdate { get; set; }
        public ObservableCollection<RoleInitModel> RoleInit { get; set; }
        public ObservableCollection<DisplayItemsModel> WeighingDisplayItems { get; set; }

        public DisplayItemsStatic()
        {
            IsHomeUpdate = true;
            IsDataUpdate = true;
            if (!File.Exists(GlobalStatic.RoleInitPath))
            {
                RoleInit = new ObservableCollection<RoleInitModel>();
            }
            else
            {
                try
                {
                    RoleInit = JsonSerializer.Deserialize<ObservableCollection<RoleInitModel>>(File.ReadAllText(GlobalStatic.RoleInitPath), GlobalStatic.Options);
                }
                catch (Exception)
                {
                    RoleInit = new ObservableCollection<RoleInitModel>();
                }
            }
            if (!File.Exists(GlobalStatic.DisplayItemPath))
            {
                WeighingDisplayItems = new ObservableCollection<DisplayItemsModel>();
            }
            else
            {
                try
                {
                    WeighingDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.DisplayItemPath), GlobalStatic.Options);
                }
                catch (Exception)
                {
                    WeighingDisplayItems = new ObservableCollection<DisplayItemsModel>();
                }
            }
        }

        public void SaveRoleInit() => File.WriteAllText(GlobalStatic.RoleInitPath, JsonSerializer.Serialize(RoleInit, GlobalStatic.Options));

        public void SaveDisplayItem() => File.WriteAllText(GlobalStatic.DisplayItemPath, JsonSerializer.Serialize(WeighingDisplayItems, GlobalStatic.Options));
    }
}
